package com.blog.controller;


import com.blog.common.result.R;
import com.blog.common.result.RPage;
import com.blog.entity.dto.ArticleDTO;
import com.blog.entity.dto.ArticleQueryDTO;
import com.blog.entity.po.Article;
import com.blog.entity.vo.ArticleVO;
import com.blog.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-24
 */

/**
 * 文章控制器
 */
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    /**
     * 获取文章列表
     *
     * @return
     */
    @GetMapping
    public R<RPage<ArticleVO>> articleList(ArticleQueryDTO dto) {
        RPage<ArticleVO> vosPage = articleService.articleList(dto);
        return R.success(vosPage);
    }

    /**
     * 根据id获取文章详情
     *
     * @param id 文章id
     * @return
     */
    @GetMapping("{id}")
    public R<ArticleVO> getById(@PathVariable Long id) {
        ArticleVO vo = articleService.getArticleById(id);
        return R.success(vo);
    }

    @PutMapping("{id}")
    public R updateStatusById(@PathVariable Long id) {
        log.info("修改文章状态:{}", id);
        articleService.updateStatusById(id);
        return R.success();
    }


    @PutMapping
    public R editArticle(@RequestBody ArticleDTO dto) {
        log.info("修改文章:{}", dto);
        articleService.editArticle(dto);
        return R.success();
    }

    @PostMapping
    public R saveArticle(@RequestBody ArticleDTO dto) {
        log.info("新增文章:{}", dto);
        articleService.saveArticle(dto);
        return R.success();
    }


}
